package com.calis.ill.server.controller;

import java.math.BigDecimal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.calis.ill.order.service.api.UserService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;

	ObjectMapper mapper = new ObjectMapper();

	@PostMapping("/add")
	public void register(@RequestBody String userInfo) throws Exception {

		Map<String, String> rst = mapper.readValue(userInfo, new TypeReference<Map<String, String>>() {
		});
		String account = rst.get("account");
		String username = rst.get("username");
		String password = rst.get("password");
		BigDecimal balance = new BigDecimal(rst.get("balance"));

		userService.add(account, username, password, balance);
	};
	
	@GetMapping("/{account}")
	public String getUser(@PathVariable String account){
		String rst = "";
		try {
			rst = userService.getUser(account);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rst;
	}; 
}
